//
//  DHNewsView.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/5/6.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHNewsView.h"
#import "DHMagazine_LargeCell.h"
#import "DHMagazine_NormalCell.h"
#import "DHNewsDetailController.h"

#pragma mark - NewsTabButton
@interface NewsTabButton : UIButton

@property (strong, nonatomic) UIView *bgView; //背景图片

@end

@implementation NewsTabButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        if (!self.bgView) {
            self.bgView = [[UIView alloc] initWithFrame:CGRectMake(5, 0, frame.size.width - 10, frame.size.height)];
            self.bgView.backgroundColor = [UIColor colorWithHexString:@"#5F657B"];
            [self addSubview:self.bgView];
            self.bgView.layer.cornerRadius = (frame.size.height) / 2;
        }
    }
    return self;
}

- (void)setSelected:(BOOL)selected{
    [super setSelected:selected];
    
    if (selected) {
        self.bgView.hidden = NO;
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }else{
        self.bgView.hidden = YES;
        [self setTitleColor:[UIColor colorWithHexString:@"#575757"] forState:UIControlStateNormal];
    }
}

@end

#pragma mark - DHNewsView

@interface DHNewsView()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *newsTableView; //内容
@property (strong, nonatomic) NSMutableArray *itemArr; //数据
@property (strong, nonatomic) UIScrollView *tabScrollView; //顶部切换

@property (assign, nonatomic) NSInteger pageIndex; //页数
@property (strong, nonatomic) NSString *type; //资讯类型

@end

@implementation DHNewsView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        if (!self.tabScrollView) {
            self.tabScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 36)];
//            [self.tabScrollView setContentSize:CGSizeMake(80 * 5, 36)];
            [self.tabScrollView setShowsVerticalScrollIndicator:NO];
            [self.tabScrollView setShowsHorizontalScrollIndicator:NO];
            
            CGFloat width = kScreenWidth / 5;
            NSArray *titleArr = @[@"热门",@"穿搭",@"运动",@"潮流",@"商务"];
            for (int i = 0; i < 5; i++) {
                NewsTabButton *button = [[NewsTabButton alloc] initWithFrame:CGRectMake(width * i, 3, width, 30)];
                [button setTitle:titleArr[i] forState:UIControlStateNormal];
                [button setTitleColor:[UIColor colorWithHexString:@"#575757"] forState:UIControlStateNormal];
                button.titleLabel.font = [UIFont systemFontOfSize:16];
                [self.tabScrollView addSubview:button];
                button.tag = i;
                [button addTarget:self action:@selector(changeTab:) forControlEvents:UIControlEventTouchUpInside];
                if (i == 0) {
                    [button setSelected:YES];
                }else{
                    [button setSelected:NO];
                }
            }
            [self addSubview:self.tabScrollView];
        }
        if (!self.newsTableView) {
            self.newsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 36, frame.size.width, frame.size.height) style:UITableViewStylePlain];
            [self.newsTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
            self.newsTableView.dataSource = self;
            self.newsTableView.delegate = self;
            self.newsTableView.showsVerticalScrollIndicator = NO;
            [self.newsTableView registerNib:[UINib nibWithNibName:@"DHMagazine_LargeCell" bundle:nil] forCellReuseIdentifier:kDHMagazine_LargeCell];
            [self.newsTableView registerNib:[UINib nibWithNibName:@"DHMagazine_NormalCell" bundle:nil] forCellReuseIdentifier:kDHMagazine_NormalCell];
            self.newsTableView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewsData)];
            self.newsTableView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreNewsData)];
            [self addSubview:self.newsTableView];
        }
    }
    return self;
}

- (NSMutableArray *)itemArr{
    if (!_itemArr) {
        _itemArr = [NSMutableArray array];
    }
    return _itemArr;
}

- (void)changeTab:(UIButton *)button{
    for (UIButton *btn in self.tabScrollView.subviews) {
        if (btn.tag == button.tag) {
            [btn setSelected:YES];
        }else{
            [btn setSelected:NO];
        }
    }
    NSLog(@"%ld",(long)button.tag);
    switch (button.tag) {
        case 0:{
            self.type = nil;
            break;
        }
        case 1:{
            self.type = @"穿搭";
            break;
        }
        case 2:{
            self.type = @"运动";
            break;
        }
        case 3:{
            self.type = @"潮流";
            break;
        }
        case 4:{
            self.type = @"商务";
            break;
        }
            
        default:
            break;
    }
    [self.newsTableView.mj_header beginRefreshing];
}

- (void)loadNewsData{
    self.pageIndex = 0;
    [self.newsTableView.mj_footer resetNoMoreData];
    NSDictionary *params;
    if (self.type) {
        params = @{@"type":self.type,@"pageIndex":@(self.pageIndex),@"pageSize":@(20)};
    }else{
        params = @{@"pageIndex":@(self.pageIndex),@"pageSize":@(20)};
    }
    [[DHNetAPIClient shareDHNetAPIClient] request_articlesParams:params andBlock:^(id data, NSError *error) {
        [self.newsTableView.mj_header endRefreshing];
        if (data) {
            [self.itemArr removeAllObjects];
            [self.itemArr addObjectsFromArray:[data objectForKey:@"articles"]];
            if (self.itemArr.count < 20) {
                [self.newsTableView.mj_footer endRefreshingWithNoMoreData];
            }
            self.pageIndex += 1;
            [self.newsTableView reloadData];
        }
    }];
}

- (void)loadMoreNewsData{
    NSDictionary *params;
    if (self.type) {
        params = @{@"type":self.type,@"pageIndex":@(self.pageIndex),@"pageSize":@(20)};
    }else{
        params = @{@"pageIndex":@(self.pageIndex),@"pageSize":@(20)};
    }
    [[DHNetAPIClient shareDHNetAPIClient] request_articlesParams:params andBlock:^(id data, NSError *error) {
        [self.newsTableView.mj_footer endRefreshing];
        if (data) {
            [self.itemArr addObjectsFromArray:[data objectForKey:@"articles"]];
            if (self.itemArr.count < 20 * (self.pageIndex + 1)) {
                [self.newsTableView.mj_footer endRefreshingWithNoMoreData];
            }
            self.pageIndex += 1;
            [self.newsTableView reloadData];
        }
    }];
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row];
    if ([[model objectForKey:@"layout"] isEqualToString:@"大矩形"]) {
        return 210;
    }else{
        return 100;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = self.itemArr[indexPath.row];
    if ([[model objectForKey:@"layout"] isEqualToString:@"大矩形"]) {
        DHMagazine_LargeCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHMagazine_LargeCell forIndexPath:indexPath];
        [cell updateCell:model];
        return cell;
    }else{
        DHMagazine_NormalCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHMagazine_NormalCell forIndexPath:indexPath];
        [cell updateCell:model];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row];
    DHNewsDetailController *vc = [[DHNewsDetailController alloc] initWithArticleID:[model objectForKey:@"_id"]];
    vc.hidesBottomBarWhenPushed = YES;
//    __weak __typeof(self) weakSelf = self;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}


@end
